﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using DP.RecallDeal.Interface.Requests;

namespace DP.RecallDeal.Interface.Parsers
{
    public class ParserService<T> : IParser<T>
    {
        public IRequest Request {get; set;}

        public ParserService(IRequest request)
        {
            this.Request = request;
        }

        public ParserService()
        { }        

        public virtual T ParseData()
        {
            Stream stream = null;
            try
            {
                stream = Request.GetResponseStreamByPOST();

                XmlSerializer serializer = new XmlSerializer(typeof(T));
                object data = (T)serializer.Deserialize(stream);

                return (T)data;
            }
            catch (Exception ex)
            { }
            finally {
                if (stream != null)
                    stream.Dispose();
            }
            return default(T);
        }
    }
}
